package group.oneonetwo.hotelintelligencesystem.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zlk
 * @description swagger配置类
 * 通过swagger给一些比较哪里接的属性或接口增加注释信息
 * 实现接口星悦糖档实时更新，方便前后端人员实时交互
 * 方便在线测试
 */
@Configuration  //配置到springboot
@EnableSwagger2  //开启Swagger2
public class SwaggerConfig {

    /**
     * 自定义标识
     */
//    @Value("${jwt.header}")
    public final static String TOKEN_HEADER = "Authorization";

    /**
     * 前缀
     */
//    @Value("${jwt.token-prefix}")
    public final static String TOKEN_PREFIX = "Bearer";

    //配置了Swagger的Docket的Bean实例
    @Bean
    public Docket createRestApi() {
        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        ticketPar.name(TOKEN_HEADER).description("token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .defaultValue(TOKEN_PREFIX + " ")
                .required(true)
                .build();
        pars.add(ticketPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式，basePackage指定要扫描的包
                .apis(RequestHandlerSelectors.basePackage("group.oneonetwo.hotelintelligencesystem"))
                .paths(PathSelectors.any())    //过滤什么路径，可以根据url路径设置哪些请求加入星悦糖档，忽略哪些请求
                .build()
                //配置swagger信息==>apiInfo
                .apiInfo(new ApiInfoBuilder()
                        .title("酒店智慧系统-接口管理") //设置星悦糖档的标题
                        .description("HotelIntelligenceSystem by one-one-two club") //设置星悦糖档描述
                        .version("1.0")//设置星悦糖档的版本信息
                        .contact(new Contact("Tomato_Simon"," "," "))
                        .build())
                //enable是否启动swagger，如果为false，则swagger不能在浏览器中进行访问。
                //.enable(false)
                .groupName("战神")
                .globalOperationParameters(pars);
    }

}
